<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 私海详情
 * @icon fa fa-user
 */
class Privateseainfo extends Backend
{
    protected $relationSearch = true;
    protected $SourceModel = null;
    protected $ReceiveModel = null;
    protected $AdminModel = null;
    protected $Visitmodel = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business.Business');
        $this->SourceModel = model('Business.Source');
        $this->ReceiveModel = model('Business.Receive');
        $this->AdminModel = model('Admin');
        $this->Visitmodel = model('Business.Visit');
    }
    /**
     * 查看
     */
    public function index($ids = null)
    {
        $row = $this->model->with(['scource'])->find($ids);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    /*
    回访记录
    */
    public function Visitindex()
    {
        $busid = $this->request->param('busid', 0, 'trim');
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $total = $this->Visitmodel
            ->with(['admin', 'business'])
            ->where($where)
            ->where('busid', $busid)
            ->order($sort, $order)
            ->count();
        $list = $this->Visitmodel
            ->with(['admin', 'business'])
            ->where($where)
            ->where('busid', $busid)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
        $result = array("total" => $total, "rows" => $list);

        return json($result);
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->Visitmodel->find($ids);
        if (!$row) {
            $this->error('回访记录不存在');
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $params['id']=$row['id'];
            $result = $this->Visitmodel->isUpdate()->save($params);
            if ($result === false) {
                $this->error($this->Visitmodel->getError());
            }
            $this->success('添加成功');
        }

        $this->view->assign(["row" => $row]);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = null)
    {
        $row = $this->Visitmodel->find($ids);
        if (!$row) {
            $this->error('回访记录不存在');
        }
        $result = $this->Visitmodel->destroy($ids);
        if ($result === false) {
            $this->error('删除失败');
        } else {
            $this->success('删除成功');
        }
        return $this->view->fetch();
    }
    /*
    添加
    */
    public function add($ids = null)
    {

        $busid = $this->request->param('busid', 0, 'trim');
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $params['adminid'] = $this->auth->id;
            $params['busid'] = $busid;
            $result = $this->Visitmodel->validate('common/Business/Visit')->save($params);
            if ($result === false) {
                $this->error($this->Visitmodel->getError());
            }
            $this->success('添加成功');
        }
        return $this->view->fetch();
    }

    public function admin()
    {
        $list = $this->AdminModel->all();
        $data = [];
        foreach ($list as $item) {
            $data[$item['nickname']] = $item['nickname'];
        }
        echo json_encode($data);
    }
}
